package com.fihtdc.excelwriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;

public class MlfMaker {
    private final String TAG = "MlfMaker";

    public enum AndroidPlatformType {
        Ginger, HoneyComb, IceCream
    }

    public enum ImgProfileType {
        Img_Profile_Type_1, Img_Profile_Type_2, Img_Profile_Type_3, Img_Profile_Type_4, Img_Profile_Type_5
    }

    private final String                 KEY_SHEET_MAIN   = "main";
    private final String                 KEY_PROJECT_CODE = "Project_Code";

    private String                       ExcelLoc;
    private String                       MlfLoc;
    private File                         MlfFile;

    // for SFC2030 SH82030 SFC4040 SH84040
    private String                       ProjectCodeWithChipCode;
    private String                       CdaProjectCode;

    // private String mAndroidPlatformType;
    private String                       mImgProfileType;

    private ArrayList<ArrayList<String>> ImgProfileTypeList;
    private ArrayList<String>            Img_Profile_Type_1;
    private ArrayList<String>            Img_Profile_Type_2;
    private ArrayList<String>            Img_Profile_Type_3;
    private ArrayList<String>            Img_Profile_Type_4;
    private ArrayList<String>            Img_Profile_Type_5;

    public MlfMaker(String excelLoc, String mlfLoc) {
        init(excelLoc, mlfLoc);

        try {
            Utility.Log(TAG + " start ===>");
            for (int i = 0; i < ExcelWriter.packModelList.size(); i++) {
                MlfFile = new File(MlfLoc + "/" + ExcelWriter.MlfFilenameList.get(i));
                if (MlfFile.exists()) {
                    MlfFile.delete();
                    Utility.Log("delete old mlf\n");
                }
                Utility.Log("start to gen " + ExcelWriter.MlfFilenameList.get(i) + " ===>\n");

                switch (ImgProfileType.valueOf(mImgProfileType)) {
                    case Img_Profile_Type_1:
                        Utility.Log(ImgProfileType.Img_Profile_Type_1.toString()
                                + " not supported\n");
                        System.exit(1);
                        break;
                    case Img_Profile_Type_2:
                        genPackageDownloadProfile();
                        genPackageInfo(ExcelWriter.packModelList.get(i));
                        genFirmwareFileName(ExcelWriter.packModelList.get(i));
                        genFAT();
                        genNVItem(ExcelWriter.packModelList.get(i));
                        genCefsBackFile();
                        break;
                    case Img_Profile_Type_3:
                        Utility.Log(ImgProfileType.Img_Profile_Type_3.toString()
                                + " not supported\n");
                        System.exit(1);
                        break;
                    case Img_Profile_Type_4:
                        Utility.Log(ImgProfileType.Img_Profile_Type_4.toString()
                                + " not supported\n");
                        System.exit(1);
                        break;
                    case Img_Profile_Type_5:
                        genPackageDownloadProfile();
                        genPackageInfoMain(ExcelWriter.packModelList.get(i));
                        genPackageInfo(ExcelWriter.packModelList.get(i));
                        break;
                }

                Utility.Log("<=== " + ExcelWriter.MlfFilenameList.get(i) + " generated\n");
            }
            Utility.Log(TAG + " <===");
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void init(String excelLoc, String mlfLoc) {
        ExcelLoc = excelLoc;
        MlfLoc = mlfLoc;

        int rowIndexOfProjectCode;
        int colIndexOfProjectCode;

        ImgProfileTypeList = new ArrayList<ArrayList<String>>();
        Img_Profile_Type_1 = new ArrayList<String>();
        Img_Profile_Type_2 = new ArrayList<String>();
        Img_Profile_Type_3 = new ArrayList<String>();
        Img_Profile_Type_4 = new ArrayList<String>();
        Img_Profile_Type_5 = new ArrayList<String>();
        boolean isImgProfileTypeListNull = true;

        try {
            // discard Warning: Cannot find drawing record - using continue record
            WorkbookSettings setting = new WorkbookSettings();
            setting.setSuppressWarnings(true);
            Workbook excel = Workbook.getWorkbook(new File(ExcelLoc), setting);
            Sheet _main = excel.getSheet(KEY_SHEET_MAIN);

            // read model in each Img_Profile_Type
            if (_main.findCell(ImgProfileType.Img_Profile_Type_1.toString()) == null) {
                Utility.Log("Cant find " + ImgProfileType.Img_Profile_Type_1 + " in "
                        + KEY_SHEET_MAIN + " sheet\n");
                ImgProfileTypeList.add(null);
            } else {
                int row = _main.findCell(ImgProfileType.Img_Profile_Type_1.toString()).getRow();
                int col = _main.findCell(ImgProfileType.Img_Profile_Type_1.toString()).getColumn() + 1;
                Collections.addAll(Img_Profile_Type_1,
                        _main.getCell(col, row).getContents().split(","));
                ImgProfileTypeList.add(Img_Profile_Type_1);
                isImgProfileTypeListNull = false;
            }
            if (_main.findCell(ImgProfileType.Img_Profile_Type_2.toString()) == null) {
                Utility.Log("Cant find " + ImgProfileType.Img_Profile_Type_2 + " in "
                        + KEY_SHEET_MAIN + " sheet\n");
                ImgProfileTypeList.add(null);
            } else {
                int row = _main.findCell(ImgProfileType.Img_Profile_Type_2.toString()).getRow();
                int col = _main.findCell(ImgProfileType.Img_Profile_Type_2.toString()).getColumn() + 1;
                Collections.addAll(Img_Profile_Type_2,
                        _main.getCell(col, row).getContents().split(","));
                ImgProfileTypeList.add(Img_Profile_Type_2);
                isImgProfileTypeListNull = false;
            }
            if (_main.findCell(ImgProfileType.Img_Profile_Type_3.toString()) == null) {
                Utility.Log("Cant find " + ImgProfileType.Img_Profile_Type_3 + " in "
                        + KEY_SHEET_MAIN + " sheet\n");
                ImgProfileTypeList.add(null);
            } else {
                int row = _main.findCell(ImgProfileType.Img_Profile_Type_3.toString()).getRow();
                int col = _main.findCell(ImgProfileType.Img_Profile_Type_3.toString()).getColumn() + 1;
                Collections.addAll(Img_Profile_Type_3,
                        _main.getCell(col, row).getContents().split(","));
                ImgProfileTypeList.add(Img_Profile_Type_3);
                isImgProfileTypeListNull = false;
            }
            if (_main.findCell(ImgProfileType.Img_Profile_Type_4.toString()) == null) {
                Utility.Log("Cant find " + ImgProfileType.Img_Profile_Type_4 + " in "
                        + KEY_SHEET_MAIN + " sheet\n");
                ImgProfileTypeList.add(null);
            } else {
                int row = _main.findCell(ImgProfileType.Img_Profile_Type_4.toString()).getRow();
                int col = _main.findCell(ImgProfileType.Img_Profile_Type_4.toString()).getColumn() + 1;
                Collections.addAll(Img_Profile_Type_4,
                        _main.getCell(col, row).getContents().split(","));
                ImgProfileTypeList.add(Img_Profile_Type_4);
                isImgProfileTypeListNull = false;
            }
            if (_main.findCell(ImgProfileType.Img_Profile_Type_5.toString()) == null) {
                Utility.Log("Cant find " + ImgProfileType.Img_Profile_Type_5 + " in "
                        + KEY_SHEET_MAIN + " sheet\n");
                ImgProfileTypeList.add(null);
            } else {
                int row = _main.findCell(ImgProfileType.Img_Profile_Type_5.toString()).getRow();
                int col = _main.findCell(ImgProfileType.Img_Profile_Type_5.toString()).getColumn() + 1;
                Collections.addAll(Img_Profile_Type_5,
                        _main.getCell(col, row).getContents().split(","));
                ImgProfileTypeList.add(Img_Profile_Type_5);
                isImgProfileTypeListNull = false;
            }
            if (isImgProfileTypeListNull) {
                Utility.Log("Img_Profile_Type in main sheet is none\n");
                System.exit(1);
            }

            // read Project_Code, Android_Platform_Code, Img_Profile_Type
            if (_main.findCell(KEY_PROJECT_CODE) == null) {
                Utility.Log("Cant find " + KEY_PROJECT_CODE + "in " + KEY_SHEET_MAIN + "\n");
                System.exit(1);
            } else {
                rowIndexOfProjectCode = _main.findCell(KEY_PROJECT_CODE).getRow();
                colIndexOfProjectCode = _main.findCell(KEY_PROJECT_CODE).getColumn() + 1;
                ProjectCodeWithChipCode = _main.getCell(colIndexOfProjectCode,
                        rowIndexOfProjectCode).getContents();
                for (int i = 1; i <= ImgProfileTypeList.size(); i++) {
                    ArrayList<String> list = ImgProfileTypeList.get(i - 1);
                    if (list != null && list.contains(ProjectCodeWithChipCode)) {
                        mImgProfileType = "Img_Profile_Type_" + i;
                        Utility.Log("Img_Profile_Type of " + ProjectCodeWithChipCode + " is "
                                + mImgProfileType + "\n");
                        break;
                    }
                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void genPackageDownloadProfile() throws BiffException, IOException {
        Workbook excel = Workbook.getWorkbook(new File(ExcelLoc));
        FileWriter fw = new FileWriter(MlfFile, true);
        int row, col, bound;

        fw.write("[Package Download Profile]\n");
        Utility.Log("[Package Download Profile]\n");
        switch (ImgProfileType.valueOf(mImgProfileType)) {
            case Img_Profile_Type_1:
                Utility.Log(ImgProfileType.Img_Profile_Type_1.toString() + " not supported\n");
                break;
            case Img_Profile_Type_2:
                Sheet PkgDLProfSheet = excel.getSheet("Pkg-DL-Prof");
                int GingerRow = PkgDLProfSheet.findCell(AndroidPlatformType.Ginger.toString())
                        .getRow();
                Cell hc = PkgDLProfSheet.findCell(AndroidPlatformType.HoneyComb.toString());
                Cell ics = PkgDLProfSheet.findCell(AndroidPlatformType.IceCream.toString());
                int IcecreamRow = (hc != null) ? hc.getRow() : ((ics != null) ? ics.getRow()
                        : PkgDLProfSheet.getRows());
                int lastCol = PkgDLProfSheet.getColumns() - 1;
                Cell c = PkgDLProfSheet.findCell(ProjectCodeWithChipCode, 1, GingerRow + 1,
                        lastCol, IcecreamRow - 1, false);
                col = c.getColumn();
                row = c.getRow();
                bound = Integer.parseInt(PkgDLProfSheet.getCell(col, row + 1).getContents());
                for (int k = row + 2; k < row + bound + 2; k++) {
                    fw.write(PkgDLProfSheet.getCell(col, k).getContents() + "\n");
                    Utility.Log(PkgDLProfSheet.getCell(col, k).getContents() + "\n");
                }
                fw.write("\n");
                Utility.Log("\n");
                break;
            case Img_Profile_Type_3:
                Utility.Log(ImgProfileType.Img_Profile_Type_3.toString() + " not supported\n");
                break;
            case Img_Profile_Type_4:
                Utility.Log(ImgProfileType.Img_Profile_Type_4.toString() + " not supported\n");
                break;
            case Img_Profile_Type_5:
                Sheet PkgDLProfNVSheet = excel.getSheet("Pkg-DL-Prof_NV");
                row = PkgDLProfNVSheet.findCell(ProjectCodeWithChipCode).getRow();
                col = PkgDLProfNVSheet.findCell(ProjectCodeWithChipCode).getColumn();
                bound = Integer.parseInt(PkgDLProfNVSheet.getCell(col, row + 1).getContents());
                for (int k = row + 2; k < row + bound + 2; k++) {
                    fw.write(PkgDLProfNVSheet.getCell(col, k).getContents() + "\n");
                    Utility.Log(PkgDLProfNVSheet.getCell(col, k).getContents() + "\n");
                }
                fw.write("\n");
                Utility.Log("\n");
                break;
        }
        fw.close();
    }

    public void genPackageInfoMain(String model_code) throws BiffException, IOException {
        Workbook excel = Workbook.getWorkbook(new File(ExcelLoc));
        FileWriter fw = new FileWriter(MlfFile, true);
        Sheet PackageInfoMainSheet = excel.getSheet("PackageInfo_main");
        fw.write("[Package Info Main]\n");
        Utility.Log("[Package Info Main]\n");
        int row = PackageInfoMainSheet.findCell(ProjectCodeWithChipCode).getRow();
        int col = PackageInfoMainSheet.findCell(ProjectCodeWithChipCode).getColumn();
        int bound = Integer.parseInt(PackageInfoMainSheet.getCell(col, row + 1).getContents());
        for (int k = row + 2; k < row + bound + 2; k++) {
            Sheet ModelCodeSheet = excel.getSheet(ExcelWriter.Project_Code + "_" + model_code);
            int ColFileName = ModelCodeSheet.findCell("File Name").getColumn();
            Cell c = ModelCodeSheet.findCell(PackageInfoMainSheet.getCell(col, k).getContents());
            fw.write(PackageInfoMainSheet.getCell(col, k).getContents() + " = \""
                    + ModelCodeSheet.getCell(ColFileName, c.getRow()).getContents() + "\"\n");
            Utility.Log(PackageInfoMainSheet.getCell(col, k).getContents() + " = \""
                    + ModelCodeSheet.getCell(ColFileName, c.getRow()).getContents() + "\"\n");
        }
        fw.write("\n");
        Utility.Log("\n");
        fw.close();
    }

    public void genPackageInfo(String model_code) throws IOException, BiffException {
        Workbook excel = Workbook.getWorkbook(new File(ExcelLoc));
        FileWriter fw = new FileWriter(MlfFile, true);

        switch (ImgProfileType.valueOf(mImgProfileType)) {
            case Img_Profile_Type_1:
                Utility.Log(ImgProfileType.Img_Profile_Type_1.toString() + " not supported\n");
                break;
            case Img_Profile_Type_2:
                Utility.Log(TAG + ": [Package Info]\n");
                fw.write("[Package Info]\n");
                fw.write("/*-----------------------------------------------------------------------------\n");
                fw.write("  UPDATE_FLAG_NULL                = 0x00\n");
                fw.write("  UPDATE_FLAG_DBL                 = 0x01\n");
                fw.write("  UPDATE_FLAG_FSBL                = 0x02\n");
                fw.write("  UPDATE_FLAG_OSBL                = 0x04\n");
                fw.write("  UPDATE_FLAG_AMSS                = 0x08\n");
                fw.write("  UPDATE_FLAG_CEFS                = 0x10\n");
                fw.write("  UPDATE_FLAG_APPSBOOT            = 0x20\n");
                fw.write("  UPDATE_FLAG_WIN                 = 0x40\n");
                fw.write("  UPDATE_FLAG_DSP1                = 0x80\n");
                fw.write("  UPDATE_FLAG_DSP2                = 0x100\n");
                fw.write("  UPDATE_FLAG_ANDROID_BOOT        = 0x200\n");
                fw.write("  UPDATE_FLAG_ANDROID_SYSTEM      = 0x400\n");
                fw.write("  UPDATE_FLAG_ANDROID_USER_DATA   = 0x800\n");
                fw.write("  UPDATE_FLAG_ANDROID_RECOVERY    = 0x1000\n");
                fw.write("  UPDATE_FLAG_ANDROID_SPLASH      = 0x2000\n");
                fw.write("  UPDATE_FLAG_ANDROID_HIDDEN      = 0x4000\n");
                fw.write("  UPDATE_FLAG_ANDROID_FTM         = 0x8000\n");
                fw.write("  UPDATE_FLAG_CUSTOMER_NV         = 0x10000\n");
                fw.write("  UPDATE_FLAG_ANDROID_CDA         = 0x20000\n");
                fw.write("  UPDATE_FLAG_RESERVED_1          = 0x40000\n");
                fw.write("  UPDATE_FLAG_RESERVED_2          = 0x80000\n");
                fw.write("  UPDATE_FLAG_RESERVED_3          = 0x100000\n");
                fw.write("  UPDATE_FLAG_RESERVED_4          = 0x200000\n");
                fw.write("  UPDATE_FLAG_RESERVED_5          = 0x400000\n");
                fw.write("\n");
                fw.write("Modem: DBL + OSBL + AMSS + dsp1 = 0x8D\n");
                fw.write("Host : APPSBOOT + DSP1 + ANDROID_BOOT + ANDROID_SYSTEM"
                        + " + ANDROID_USER_DATA + ANDROID_RECOVERY = 0x1EA0\n");
                fw.write("FTM  : APPSBOOT +  ANDROID_BOOT = 0x220\n");
                fw.write("-------------------------------------------------------------------------------*/\n");

                Sheet FLAGSheet = excel.getSheet("FLAG");
                int GingerRow = FLAGSheet.findCell(AndroidPlatformType.Ginger.toString()).getRow();
                Cell hc = FLAGSheet.findCell(AndroidPlatformType.HoneyComb.toString());
                Cell ics = FLAGSheet.findCell(AndroidPlatformType.IceCream.toString());
                int nextPlatformRow = (hc != null) ? hc.getRow() : ((ics != null) ? ics.getRow()
                        : FLAGSheet.getRows());
                int lastCol = FLAGSheet.getColumns() - 1;
                Cell projectCodeInFLAG = FLAGSheet.findCell(ProjectCodeWithChipCode, 1,
                        GingerRow + 1, lastCol, nextPlatformRow - 1, false);
                fw.write("FLAG = "
                        + FLAGSheet.getCell(projectCodeInFLAG.getColumn(),
                                projectCodeInFLAG.getRow() + 1).getContents() + "\n\n");
                break;
            case Img_Profile_Type_3:
                Utility.Log(ImgProfileType.Img_Profile_Type_3.toString() + " not supported\n");
                break;
            case Img_Profile_Type_4:
                Utility.Log(ImgProfileType.Img_Profile_Type_4.toString() + " not supported\n");
                break;
            case Img_Profile_Type_5:
                Sheet ModelCodeSheet = excel.getSheet(ExcelWriter.Project_Code + "_" + model_code);
                int ColFileName = ModelCodeSheet.findCell("File Name").getColumn();
                Sheet NVPackageInfoSheet = excel.getSheet("NV_PackageInfo");
                Cell info = NVPackageInfoSheet.findCell(ExcelWriter.Project_Code, 1, 1, 1, 101,
                        false);
                int row = info.getRow();
                int col = info.getColumn();
                int info_number = Integer.parseInt(NVPackageInfoSheet.getCell(col + 1, row)
                        .getContents());
                for (int i = col + 1; i < col + info_number + 1; i++) {
                    fw.write("[Package Info " + (i - 1) + "]\n");
                    Utility.Log("[Package Info " + (i - 1) + "]\n");
                    fw.write(NVPackageInfoSheet.getCell(i, row + 1).getContents() + "\n");
                    Utility.Log(NVPackageInfoSheet.getCell(i, row + 1).getContents() + "\n");
                    String package_name = NVPackageInfoSheet.getCell(i, row + 1).getContents()
                            .split("\"")[1];
                    fw.write(NVPackageInfoSheet.getCell(i, row + 2).getContents() + "\n");
                    Utility.Log(NVPackageInfoSheet.getCell(i, row + 2).getContents() + "\n");
                    fw.write(NVPackageInfoSheet.getCell(i, row + 3).getContents() + "\n");
                    Utility.Log(NVPackageInfoSheet.getCell(i, row + 3).getContents() + "\n");

                    Sheet modelSheet = excel.getSheet(ExcelWriter.Project_Code + "_" + model_code);
                    if (package_name.equalsIgnoreCase("recovery")) {
                        int j = modelSheet.findCell("ramdisk-recovery").getRow();
                        fw.write(NVPackageInfoSheet.getCell(i, row + 4).getContents() + " = \""
                                + modelSheet.getCell(2, j).getContents() + "-0-"
                                + modelSheet.getCell(ColFileName - 1, j).getContents() + "-"
                                + model_code + "-recovery.img\"\n");
                        Utility.Log(NVPackageInfoSheet.getCell(i, row + 4).getContents() + " = \""
                                + modelSheet.getCell(2, j).getContents() + "-0-"
                                + modelSheet.getCell(ColFileName - 1, j).getContents() + "-"
                                + model_code + "-recovery.img\"\n");
                    } else if (package_name.equals("boot") || package_name.equals("BOOTIMG")) {
                        int j = modelSheet.findCell("bootconfig").getRow();
                        fw.write(NVPackageInfoSheet.getCell(i, row + 4).getContents() + " = \""
                                + modelSheet.getCell(2, j).getContents() + "-0-"
                                + modelSheet.getCell(ColFileName - 1, j).getContents() + "-"
                                + model_code + "-boot.img\"\n");
                        Utility.Log(NVPackageInfoSheet.getCell(i, row + 4).getContents() + " = \""
                                + modelSheet.getCell(2, j).getContents() + "-0-"
                                + modelSheet.getCell(ColFileName - 1, j).getContents() + "-"
                                + model_code + "-boot.img\"\n");
                    } else {
                        Cell tmp = modelSheet.findCell(package_name);
                        if (tmp != null) {
                            int j = tmp.getRow();
                            fw.write(NVPackageInfoSheet.getCell(i, row + 4).getContents() + " = \""
                                    + modelSheet.getCell(ColFileName, j).getContents() + "\"\n");
                            Utility.Log(NVPackageInfoSheet.getCell(i, row + 4).getContents()
                                    + " = \"" + modelSheet.getCell(ColFileName, j).getContents()
                                    + "\"\n");
                        } else {
                            String ImageFileCellContent = NVPackageInfoSheet.getCell(i, row + 4)
                                    .getContents();
                            fw.write((ImageFileCellContent == null)
                                    || (ImageFileCellContent.equals(""))
                                    || ImageFileCellContent.equals("IMAGE_FILE") ? "IMAGE_FILE = \"\"\n"
                                    : ImageFileCellContent + "\n");
                            Utility.Log((ImageFileCellContent == null)
                                    || (ImageFileCellContent.equals(""))
                                    || ImageFileCellContent.equals("IMAGE_FILE") ? "IMAGE_FILE = \"\"\n"
                                    : ImageFileCellContent + "\n");
                        }
                    }
                    fw.write("\n");
                    Utility.Log("\n");
                }
                break;
        }
        fw.close();
    }

    public void genFirmwareFileName(String model_code) throws BiffException, IOException {
        Workbook excel = Workbook.getWorkbook(new File(ExcelLoc));
        FileWriter fw = new FileWriter(MlfFile, true);

        fw.write("[Firmware File Name]\n");
        Utility.Log("[Firmware File Name]\n");
        Sheet ModelCodeSheet = excel.getSheet(ExcelWriter.Project_Code + "_" + model_code);
        int col = ModelCodeSheet.findCell("File Name").getColumn();
        int row = ModelCodeSheet.findCell("mprg7x30hex").getRow();
        fw.write("FILENAME_EFLASH_HEX = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        row = ModelCodeSheet.findCell("nprg7x30hex").getRow();
        fw.write("FILENAME_FLASH_HEX = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        fw.write("FILENAME_FLASH_BIN = \"xxx.bin\"\n");

        row = ModelCodeSheet.findCell("partitionmbn").getRow();
        fw.write("FILENAME_PARTITION = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        row = ModelCodeSheet.findCell("partitionbin").getRow();
        fw.write("FILENAME_PARTITION_BIN = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        fw.write("FILENAME_OSBLHD = \"osblhd.mbn\"\n");

        row = ModelCodeSheet.findCell("osbl").getRow();
        fw.write("FILENAME_OSBL = \"" + ModelCodeSheet.getCell(col, row).getContents() + "\"\n");

        fw.write("FILENAME_DBLHD = \"dblhd.mbn\"\n");

        row = ModelCodeSheet.findCell("dbl").getRow();
        fw.write("FILENAME_DBL = \"" + ModelCodeSheet.getCell(col, row).getContents() + "\"\n");

        fw.write("FILENAME_AMSSHD = \"amsshd.mbn\"\n");

        fw.write("FILENAME_AMSS = \"" + ModelCodeSheet.getCell(col, row + 1).getContents() + "\"\n");

        fw.write("FILENAME_CEFS = \"cefs_modem.mbn\"\n");

        row = ModelCodeSheet.findCell("adsp").getRow();
        fw.write("FILENAME_DSP1 = \"" + ModelCodeSheet.getCell(col, row).getContents() + "\"\n");

        fw.write("FILENAME_DSP2 = \"dsp2.mbn\"\n");

        fw.write("FILENAME_APPSBOOTHD = \"appsboothd.mbn\"\n");

        row = ModelCodeSheet.findCell("emmcboot").getRow();
        fw.write("FILENAME_APPSBOOT = \"" + ModelCodeSheet.getCell(col, row).getContents() + "\"\n");

        row = ModelCodeSheet.findCell("ramdisk").getRow();
        String[] _tmp = ModelCodeSheet.getCell(col, row).getContents().split("-");
        row = ModelCodeSheet.findCell("kernel").getRow();
        String _tmp2 = ModelCodeSheet.getCell(col - 1, row).getContents();
        fw.write("FILENAME_ANDROID_BOOT = \"" + _tmp[0] + "-" + _tmp[1] + "-" + _tmp2 + "-"
                + _tmp[3] + "-boot.img\"\n");

        row = ModelCodeSheet.findCell("system").getRow();
        fw.write("FILENAME_ANDROID_SYSTEM = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        fw.write("FILENAME_ANDROID_USR_DATA = \"" + ExcelWriter.Project_Code
                + "-0-VV-MM-userdata.img.ext3\"\n");

        row = ModelCodeSheet.findCell("ramdisk-recovery").getRow();
        fw.write("FILENAME_ANDROID_RECOVERY = \""
                + ModelCodeSheet.getCell(col, row).getContents().split("-ram")[0]
                + "-recovery.img\"\n");

        row = ModelCodeSheet.findCell("splash").getRow();
        fw.write("FILENAME_ANDROID_SPLASH = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        row = ModelCodeSheet.findCell("hidden").getRow();
        fw.write("FILENAME_ANDROID_HIDDEN = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        row = ModelCodeSheet.findCell("ftm").getRow();
        fw.write("FILENAME_ANDROID_FTM = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");

        row = ModelCodeSheet.findCell("loadpt").getRow();
        fw.write("FILENAME_LOADPT = \"" + ModelCodeSheet.getCell(col, row).getContents() + "\"\n");

        row = ModelCodeSheet.findCell("NV_cust").getRow();
        fw.write("FILENAME_NV_CUST = \"" + ModelCodeSheet.getCell(col, row).getContents() + "\"\n");

        row = ModelCodeSheet.findCell("cda").getRow();
        fw.write("FILENAME_ANDROID_CDA = \"" + ModelCodeSheet.getCell(col, row).getContents()
                + "\"\n");
        CdaProjectCode = ModelCodeSheet.getCell(col, row).getContents().split("-")[0];

        String[] reserved = new String[5];
        for (int k = 1; k <= 5; k++) {
            Cell c = ModelCodeSheet.findCell("reserved" + k);
            if (c != null) {
                row = c.getRow();
                reserved[k - 1] = ModelCodeSheet.getCell(col, row).getContents();
            } else {
                reserved[k - 1] = "";
            }
        }
        fw.write("FILENAME_RESERVED1 = \"" + reserved[0] + "\"\n");
        fw.write("FILENAME_RESERVED2 = \"" + reserved[1] + "\"\n");
        fw.write("FILENAME_RESERVED3 = \"" + reserved[2] + "\"\n");
        fw.write("FILENAME_RESERVED4 = \"" + reserved[3] + "\"\n");
        fw.write("FILENAME_RESERVED5 = \"" + reserved[4] + "\"\n");

        Sheet FFNSheet = excel.getSheet("Firmware-File-Name");
        int i2 = FFNSheet.findCell(ProjectCodeWithChipCode).getColumn();
        int j2 = FFNSheet.findCell(ProjectCodeWithChipCode).getRow();
        fw.write(FFNSheet.getCell(i2, j2 + 2).getContents().replace("[SwModel]", model_code)
                + "\n\n");

        fw.close();
    }

    public void genFAT() throws BiffException, IOException {
        Workbook excel = Workbook.getWorkbook(new File(ExcelLoc));
        FileWriter fw = new FileWriter(MlfFile, true);

        fw.write("[FAT]\n");
        Utility.Log("[FAT]");

        Sheet FATSheet = excel.getSheet("FAT");
        int i = FATSheet.findCell(ProjectCodeWithChipCode).getColumn();
        int j = FATSheet.findCell(ProjectCodeWithChipCode).getRow();
        for (int k = j + 2; k < j + 2 + Integer.parseInt(FATSheet.getCell(i, j + 1).getContents()); k++) {
            fw.write(FATSheet.getCell(i, k).getContents() + "\n");
        }
        fw.write("\n");
        fw.close();
    }

    public void genNVItem(String model_code) throws IOException {
        FileWriter fw = new FileWriter(MlfFile, true);

        fw.write("[NV Item]\n");
        fw.write("QCN FILENAME  = \"NV.xml\"\n");
        fw.write("NV DEFINITION = \"NvDefinition.xml\"\n");
        fw.write("CUSTOM_NV     = \"" + CdaProjectCode + "_" + model_code + "_NV.xml\"\n\n");
        fw.close();
    }

    public void genCefsBackFile() throws IOException {
        FileWriter fw = new FileWriter(MlfFile, true);

        fw.write("[CEFS BACKUP FILE]\n");
        Utility.Log("[CEFS BACKUP FILE]\n");

        fw.write("FILE NUMBER = 0\n");
        fw.write("FILE NAME   = NULL\n");
        fw.close();
    }
}
